package com.corpgovernment.job.dao.mapper;

import com.corpgovernment.job.dao.dataobject.CountDo;
import com.corpgovernment.job.dao.entity.db.JobDo;
import com.corpgovernment.job.enums.DeletedEnum;
import com.corpgovernment.organization.common.mybatis.TkMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.apache.ibatis.session.RowBounds;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ldma
 * @date 2024/1/3
 */
public interface JobMapper extends TkMapper<JobDo> {

    default void create(JobDo jobDo) {
        insertSelective(jobDo);
    }

    default void removeById(Long id) {
        if (id == null) {
            return;
        }
        JobDo jobDo = new JobDo();
        jobDo.setId(id);
        jobDo.setDeleted(DeletedEnum.TRUE.getCode());
        updateByPrimaryKeySelective(jobDo);
    }

    default void update(JobDo jobDo) {
        updateByPrimaryKeySelective(jobDo);
    }

    default void updateJobTypeToUnclassified(Long jobTypeId) {
        if (jobTypeId == null) {
            return;
        }
        JobDo jobDo = new JobDo();
        jobDo.setJobTypeId(1L);
        Example example = new Example(JobDo.class);
        example.createCriteria().andEqualTo("jobTypeId", jobTypeId);
        updateByExampleSelective(jobDo, example);
    }

    default List<JobDo> listAll() {
        Example example = new Example(JobDo.class);
        example.createCriteria().andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        return selectByExample(example);
    }

    default List<JobDo> listByJobTypeId(Long jobTypeId) {
        if (jobTypeId == null) {
            return new ArrayList<>(0);
        }
        Example example = new Example(JobDo.class);
        example.createCriteria().andEqualTo("deleted", DeletedEnum.FALSE.getCode()).andEqualTo("jobTypeId", jobTypeId);
        example.orderBy("id").desc();
        return selectByExample(example);
    }

    default JobDo getByName(String jobName) {
        if (StringUtils.isBlank(jobName)) {
            return null;
        }
        Example example = new Example(JobDo.class);
        example.createCriteria().andEqualTo("name", jobName).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        List<JobDo> jobDoList = selectByExample(example);
        return CollectionUtils.isEmpty(jobDoList) ? null : jobDoList.get(0);
    }

    default JobDo getByCode(String jobCode) {
        if (StringUtils.isBlank(jobCode)) {
            return null;
        }
        Example example = new Example(JobDo.class);
        example.createCriteria().andEqualTo("code", jobCode).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        List<JobDo> jobDoList = selectByExample(example);
        return CollectionUtils.isEmpty(jobDoList) ? null : jobDoList.get(0);
    }

    default JobDo getById(Long id) {
        if (id == null) {
            return null;
        }
        Example example = new Example(JobDo.class);
        example.createCriteria().andEqualTo("deleted", DeletedEnum.FALSE.getCode()).andEqualTo("id", id);
        List<JobDo> jobDoList = selectByExample(example);
        return CollectionUtils.isEmpty(jobDoList) ? null : jobDoList.get(0);
    }

    default List<JobDo> listByIdList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return new ArrayList<>(0);
        }
        Example example = new Example(JobDo.class);
        example.createCriteria().andIn("id", idList).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        return selectByExample(example);
    }

}
